$(function () {
    var markId = getUrlString('markid');
    var id = getUrlString('id');
    var type = getUrlString('type');
    let money2,ispw,userId2,topHeight;
    function getnative(actionurl) {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isiOS) {
            window.webkit.messageHandlers.getnative.postMessage(actionurl);
        }else {
            ikags.getnative(actionurl)
        }
    }
    function detail() {
        var data = {
            'markid':markId,
            'xiandanid':id
        };
        getData('post','/niu/apiv3/xiandan_getdetail.php',data,function (data) {
            console.log(data);
            $('.userface').attr('src',data.userface);
            $('.toyspic').attr('src',data.toyspic);

            $('.toysname,.indexName').html(data.toysname);
            let toysname = data.toysname;
            let moneyrmb = data.moneyrmb;
            let toyspic = data.toyspic;

            $('#usernick').html(data.usernick+' 出售').click(function () {
                var id = data.userid;
                window.location.href = 'dancool://member='+id;
            });
            var time = data.toys.book_time.substring(0,4) + '年' + data.toys.book_time.substring(4,6)+'月';

            $('.commodityPicture').html(escape2Html(data.toys.intro));
            if (data.toys.book_time === '0'){
                $('.fssj').html('无');
            } else {
                $('.fssj').html(time);
            }
            if (data.backmoneyhp > 0){
                $('.choumoney2').html(data.backmoneyhp+ 'HP/金币');
            } else {
                $('.choumoney2').html(data.backmoneyyin+ '银币');
            }

            if (data.backmoneyhp === 0 && data.backmoneyyin === 0) {
                $('.choumoney2').html('特殊商品');
            }

            if (data.cuxiaoid === '1'){
                $('.choumoney2').html('不可回购');
            }

            if (data.bukuan_status === '0'){
                $('.toyPrice').show();
                $('#money2').html(data.bukuan_money2);
                $('#HPPrice').html(data.bukuan_money3);
                $('#QKmoney2').html(Number(data.bukuan_money2) + Number(data.dingjin_money2));
                $('#QKHPPrice').html(Number(data.bukuan_money3) + Number(data.dingjin_money3));
                $('.spzt').css('color','#ff0000').html('需补尾款商品');
                $('.choumoney2').html('1HP/金币');
                $('.BK').show().html('该商品需补款'+data.bukuan_money2+'金币或者'+data.bukuan_money3+'HP')
            }else {
                if (data.toys.sptype === '2'){
                    $('.spzt').html('预售商品');
                } else  if (data.toys.sptype === '3'){
                    $('.spzt').html('寄售商品');
                } else {
                    $('.spzt').html('现货商品')
                }
            }

            if (data.toys.sptype === '3'){
                $('#JSSP').show()
            }else {
                $('#JSSP').hide()
            }
            // $('#macode').html(data.macode);
            $('#intro').html(data.intro);
            $('.outtime').html(subTime(data.outtime));

            if (data.isshow === '1'){
                $('#bugSaltedEgg').hide();
                $('.commodityPrice img').hide();
                $('.money2').html('￥'+data.moneyrmb / 100);
                $('#wxsapp,#provateChat').css('width','45%');
                $('#wxsapp').show().click(function () {
                    nativeCommand('dancool://wxsapp=gh_a16176d49b83|||pages/used/usedDetail/usedDetail?id='+id+'&sourt=app')
                });
            }else if (data.isshow === '0'){
                $('#wxsapp').hide();
                $('.money2').html(data.money2);
                $('#bugSaltedEgg,#provateChat').css('width','45%');
                $('#bugSaltedEgg').show();
            }else {
                $('.commodityPrice img').show();
                $('.money2').html(data.money2);
                $('#bugSaltedEgg').show();
                $('#wxsapp').show().click(function () {
                    nativeCommand('dancool://wxsapp=gh_a16176d49b83|||pages/used/usedDetail/usedDetail?id='+id+'&sourt=app')
                });
            }

            money2 = data.money2;

            if (data.uservip === 1){
                $('.userVip').show();
            }

            $('#price').html(data.toys.price);
            $('#mainseries').html(data.toys.mainseries);
            $('#maker').html(data.toys.maker);
            $('#secondseries').html(data.toys.secondseries);
            ispw = data.ispw;

            if (type === 'custom'){
                $('#provateChat').hide();
                $('#wxsapp,#bugSaltedEgg').css('width','45%')
            }





            //是否是自己的商品
            if (data.ismine === '1'){
                $('.goXianYu div').hide();
                // $('goXianYu').css('width','46%')
                $('#goToBJXG').show().css('width','95%').click(function () {
                    window.location.href="dancool://fullweb="+urlView+"my/myEggPoorUpData.html?id="+id+"&markid="+markId;
                })
            }else {
                $('#goToBJXG').hide()
            }



            getData('post','/niu/apiv3/member_userinfo.php',{'markid':markId},function (data) {
                console.log(data);
                userId2 = data.data.id
            });
            //调往私聊
            $('#provateChat').click(function () {
                var userId = data.userid;
                var parameter = {
                    'markid':markId,
                    'blockuserid1':userId,
                    'blockuserid2':userId2
                };
                getData('post','/niu/apiv3/weibo_blackuser.php?method=itemcheckstate',parameter,function (data) {
                    console.log(data);
                    if (data.isblock === "0"){
                        // var nick = data.data.nick;
                        let toUserName = 'djuser_'+userId;
                        getnative('dancool://message_enterSingleConversation='+toUserName+'');
                        getnative('dancool://message_createSingleCustomMessage='+toUserName+'|||xdid='+id+'&title='+toysname+'&price='+(moneyrmb) / 100+'&toyspic='+toyspic+'')
                        window.location.href = 'dancool://fullweb='+urlView+'messagesV2/sessionList.html?toUserName='+toUserName
                    }else {
                        laymsg('您两人处于黑名单状态下')
                    }
                });

            });

            topHeight = $('.commodityPic').height() + 70;
        })
    }
    detail();

    $('#closeIndex').click(function () {
        nativeCommand('dancool://activity=fin')
    });
    $(window).scroll(function () {
        var top = $(window).scrollTop();//滚动条距离顶部的高度
        if (top > topHeight){
            $('.indexTitleName').fadeIn()
        } else {
            $('.indexTitleName').fadeOut()
        }
    });

    $('#hgjgInfo').click(function () {
        showModal('提示','常规商品，用户可以主动以系统显示价格将商品回购销售给系统，商品回购定价根据该商品抽取蛋机的单抽抽取价格统一制定。','','确定');
    });



    //买咸蛋
    function buySaltedEgg(pwcode) {
        var data = {
            'markid':markId,
            'xiandanid':id,
            'do':1,
            'money2':money2,
            'pwcode':pwcode
        };
        getData('post','/niu/apiv3/xiandan_dobuyv2.php',data,function (data) {
            console.log(data);
            layer.closeAll('dialog');
            if(data.resultcode === '0'){
                nativeCommand("dancool://activity=fin")
            }else {
                // $('#bugSuccess').fadeOut();
                laymsg(data.msg)
            }
        })
    }
    $('#bugSaltedEgg').click(function () {
        $('.alertIndex').show()
    });

    $('#qrBtn').click(function () {
        if (ispw === "1"){
            $('.password').fadeIn()
        } else {
            buySaltedEgg('')
        }
    });
    //输入交易密码购买
    $('.password .right').click(function () {
        var pwcode = $('#pwcode').val();
        if (pwcode === ''){
            laymsg('交易密码不能为空');
            return
        }
        buySaltedEgg(pwcode);
        $('.password').fadeOut()

    });

    $('#closeAlert').click(function () {
        $('.alertIndex').hide()
    })

    //取消交易密码弹窗
    $('.password .left').click(function () {
        $('.password').fadeOut()
    });
});
